/*
 */

package com.railway.files.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.util.DateConvertUtils;

import com.railway.common.base.BaseManager;
import com.railway.common.base.EntityDao;
import com.railway.common.constants.SysConstant;
import com.railway.files.dao.IFilesDao;
import com.railway.files.model.Files;
import com.railway.files.service.IFilesManager;
import com.railway.files.vo.query.FilesQuery;
import com.railway.news.dao.ICategoryDao;
import com.railway.news.model.Category;

/**
 * @author jiangcheng
 * @version 1.0
 * @since 1.0
 */

@Service("filesManager")
@Transactional
public class FilesManagerImpl extends BaseManager<Files, java.lang.String>
		implements IFilesManager {
	private ICategoryDao categoryDao;
	private IFilesDao filesDao;

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写 */
	public void setFilesDao(IFilesDao dao) {
		this.filesDao = dao;
	}

	public EntityDao getEntityDao() {
		return (EntityDao) this.filesDao;
	}

	@Transactional(readOnly = true)
	public Page findPage(FilesQuery query) {

		Page p = filesDao.findPage(query);

		try {

			if ((SysConstant.CATEGORY_TYPE_SOFTWARE_MANAGEMENT).equals(query.getFileTypes())){

				List list = p.getResult();

				if (list != null) {

					for (int i = 0; i < list.size(); i++) {

						Files f = (Files) list.get(i);
             
						
						Category c = (Category) categoryDao.getById(f.getObjId());
						if (c == null) {
							continue;
						}
		

						f.setCatname(c.getNames());

					}
				}
			}else if ((SysConstant.CATEGORY_TYPE_PHOTO_MANAGEMENT).equals(query.getFileTypes())){
				List list = p.getResult();

				if (list != null) {

					for (int i = 0; i < list.size(); i++) {
						
						Files f = (Files) list.get(i);             
			            f.setNames(f.getNames() == null?"":f.getNames());
			            f.setMemo(f.getMemo() == null?"":f.getMemo());
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;

	}

	@Transactional(readOnly = true)
	public Files getById(java.lang.String id) {
		return super.getById(id);
	}

	@Transactional
	public void removeById(java.lang.String id) {
		super.removeById(id);
	}

	/** 插入数据 */
	@Transactional
	public void save(Files entity) {
		super.save(entity);
	}

	/** 更新数据 */
	@Transactional
	public void update(Files entity) {

		super.update(entity);
	}

	@Transactional(readOnly = true)
	public boolean isUnique(Files entity, String uniquePropertyNames) {
		return super.isUnique(entity, uniquePropertyNames);
	}

	@Transactional(readOnly = true)
	public List<Files> findAll() {
		return super.findAll();
	}

	@Transactional
	public void saveOrUpdate(Files entity) {
		super.saveOrUpdate(entity);
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}


	public Page findPageByLike(FilesQuery query) {
		Page p = filesDao.findPageByLike(query);

		try {

			if ((SysConstant.CATEGORY_TYPE_SOFTWARE_MANAGEMENT).equals(query.getFileTypes())){

				List list = p.getResult();

				if (list != null) {

					for (int i = 0; i < list.size(); i++) {

						Files f = (Files) list.get(i);
             
						
						Category c = (Category) categoryDao.getById(f.getObjId());
						if (c == null) {
							continue;
						}
			

						f.setCatname(c.getNames());

					}
				}
			}else if ((SysConstant.CATEGORY_TYPE_PHOTO_MANAGEMENT).equals(query.getFileTypes())){
				List list = p.getResult();

				if (list != null) {

					for (int i = 0; i < list.size(); i++) {
						
						Files f = (Files) list.get(i);             
			            f.setNames(f.getNames() == null?"":f.getNames());
			            f.setMemo(f.getMemo() == null?"":f.getMemo());
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;

	}

}
